package com.project.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }


    //创建队列  (默认)
    @Bean
    public Queue infoQueue(){
        return new Queue("infoQueue");
    }
    //  主题队列

    //创建交换机
    @Bean
    public DirectExchange directExchangeUser(){
        return new DirectExchange("directExchangeInfo");
    }
    //主题交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }

    //交换机与队列进行绑定
    @Bean
    public Binding userQueueToDirectExchangeUser(Queue infoQueue,DirectExchange directExchangeUser){
        return    BindingBuilder.bind(infoQueue).to(directExchangeUser).with("info.k");
    }

    //主题
    @Bean
    public Binding userQueueTopicExchange(Queue infoQueueTopic,TopicExchange topicExchange){
        return    BindingBuilder.bind(infoQueueTopic).to(topicExchange).with("info.#");
    }


    @Bean
    public Binding j187QueueToDirectExchangeJ187(Queue j187Queue,DirectExchange directExchangeJ187){
        return BindingBuilder.bind(j187Queue).to(directExchangeJ187).with("j187");
    }
    @Bean
    public Binding deadQueueToDirectExchangeDead(Queue deadQueue,DirectExchange directExchangeDead){
        return BindingBuilder.bind(deadQueue).to(directExchangeDead).with("j187.dead");
    }
}
